package example.上下左右;

public class Demo2 {
    public boolean exist2(char[][] board, String word) {
        char[] words = word.toCharArray();
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[0].length; j++) {
                if (dfs(board, words, i, j, 0))
                    return true;
            }
        }
        return false;
    }

    boolean dfs(char[][] board, char[] word, int i, int j, int k) {
        if (i >= board.length || i < 0 || j >= board[0].length || j < 0 || board[i][j] != word[k])
            return false;
        if (k == word.length - 1)
            return true;
        board[i][j] = '\0';
        //每一次 方法1||方法2||方法3   执行完了方法1，才会执行方法2....
        boolean res = dfs(board, word, i + 1, j, k + 1) || dfs(board, word, i - 1, j, k + 1) ||
                dfs(board, word, i, j + 1, k + 1) || dfs(board, word, i, j - 1, k + 1);
        board[i][j] = word[k];
        return res;
    }


    public static boolean exist(char[][] board, String word) {
        char c = word.charAt(0);
        int x = board.length;
        int y = board[0].length;
        if (x * y < word.length()) {
            return false;
        }
        for (int i = 0; i < x; i++) {
            for (int j = 0; j < y; j++) {
                if (board[i][j] == c) {
                    if (isTrue(board, word, i, j, x, y, 0, true, true, true, true)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public static boolean isTrue(char[][] board, String word,
                                 int i, int j,
                                 int x, int y, int count,
                                 //permit-all
                                 boolean u, boolean d, boolean r, boolean l) {
        if (i >= x || j >= y || i < 0 || j < 0 || board[i][j] != word.charAt(count)) {
            return false;
        }
        if (count == word.length() - 1) {
            return true;
        }

        if (!u) {
            return
                    // r
                    isTrue(board, word, i + 1, j, x, y, count + 1, true, true, true, false) ||
                            //l
                            isTrue(board, word, i - 1, j, x, y, count + 1, true, true, false, true) ||
                            // d
                            isTrue(board, word, i, j + 1, x, y, count + 1, false, true, true, true);
//                            //u
//                            isTrue(board, word, i, j - 1, x, y, count + 1, true, false, true, true, i, j);
        } else if (!d) {
            return
                    // r
                    isTrue(board, word, i + 1, j, x, y, count + 1, true, true, true, false) ||
                            //l
                            isTrue(board, word, i - 1, j, x, y, count + 1, true, true, false, true) ||
                            // d
//                            isTrue(board, word, i, j + 1, x, y, count + 1, false, true, true, true, i, j) ||
                            //u
                            isTrue(board, word, i, j - 1, x, y, count + 1, true, false, true, true);
        } else if (!l) {
            return
                    // r
                    isTrue(board, word, i + 1, j, x, y, count + 1, true, true, true, false) ||
                            //l
//                            isTrue(board, word, i - 1, j, x, y, count + 1, true, true, false, true, i, j) ||
                            // d
                            isTrue(board, word, i, j + 1, x, y, count + 1, false, true, true, true) ||
                            //u
                            isTrue(board, word, i, j - 1, x, y, count + 1, true, false, true, true);
        } else if (!r) {
            return
                    // r
//                    isTrue(board, word, i + 1, j, x, y, count + 1, true, true, true, false, i, j) ||
                    //l
                    isTrue(board, word, i - 1, j, x, y, count + 1, true, true, false, true) ||
                            // d
                            isTrue(board, word, i, j + 1, x, y, count + 1, false, true, true, true) ||
                            //u
                            isTrue(board, word, i, j - 1, x, y, count + 1, true, false, true, true);
        } else {
            return
                    // r
                    isTrue(board, word, i + 1, j, x, y, count + 1, true, true, true, false) ||
                            //l
                            isTrue(board, word, i - 1, j, x, y, count + 1, true, true, false, true) ||
                            // d
                            isTrue(board, word, i, j + 1, x, y, count + 1, false, true, true, true) ||
                            //u
                            isTrue(board, word, i, j - 1, x, y, count + 1, true, false, true, true);
        }

    }

    public static void main(String[] args) {

        char[][] chars =
//                new char[][]{{'a'}};
                new char[][]{{'A', 'B', 'C', 'E'}, {'S', 'F', 'C', 'S'}, {'A', 'D', 'E', 'E'}};
        boolean exist =
                exist(chars, "ABCCSE");
//                exist(chars, "a");
        System.out.println(exist);
    }
}
